package com.cssw.fyzb.web.service;

import com.cssw.fyzb.base.web.domain.AjaxData;
import com.cssw.fyzb.base.web.domain.AjaxPage;
import com.cssw.fyzb.core.dao.system.FuncDao;
import com.cssw.fyzb.core.model.dodb.SysFunc;
import com.cssw.fyzb.web.util.FuncUtil;
import com.cssw.fyzb.web.vo.FuncVo;
import com.ydn.dbframe.kit.StrKit;
import com.ydn.dbframe.plugin.activerecord.Db;
import com.ydn.dbframe.plugin.activerecord.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: yangyiqun
 * @Date: 2020/10/10 15:09
 */
@Service
public class FuncService {

    @Autowired
    private FuncDao funcDao;


    /**
     * 查询菜单列表
     *
     * @param page
     * @param limit
     * @param param
     * @return
     */
    public AjaxPage list(int page, int limit, Map param) {
        Page<SysFunc> pages = funcDao.list(page, limit, param);
        List<FuncVo> funcVos = new ArrayList<>();
        if (null != pages.getList() && pages.getList().size() != 0) {
            FuncVo funcVo = null;
            for (SysFunc func : pages.getList()) {
                funcVo = new FuncVo();
                funcVo.setFuncId(func.getFuncId());
                funcVo.setFuncCode(func.getFuncCode());
                funcVo.setFuncName(func.getFuncName());
                funcVo.setFuncIcon(func.getFuncIcon());
                funcVo.setFuncType(func.getFuncType());
                funcVo.setFuncUrl(func.getFuncUrl());
                funcVo.setStatus(func.getStatus());
                funcVo.setSort(func.getSort());
                funcVo.setSupFuncId(getAllSupFuncIds(func.getSupFuncId()));
                funcVos.add(funcVo);
            }
        }

        return AjaxPage.success(funcVos, pages.getTotalPage(), pages.getTotalRow());
    }

    String getAllSupFuncIds(Long funcId) {
        List<Long> supFuncIds = new ArrayList<>();
        loopSupFuncId(supFuncIds, funcId);

        StringBuilder funcIdStr = new StringBuilder();
        for (int i = supFuncIds.size() - 1; i >= 0; i--) {
            funcIdStr.append(supFuncIds.get(i));

            if (i != 0) {
                funcIdStr.append(",");
            }
        }

        return funcIdStr.toString();
    }

    void loopSupFuncId(List<Long> supFuncIds, Long supFuncId) {
        if (null == supFuncId) {
            return;
        }

        if (StrKit.equals("0", supFuncId.toString())) {
            return;
        }

        SysFunc func = funcDao.getById(supFuncId);
        if (null == func) {
            return;
        }

        supFuncIds.add((supFuncId));

        loopSupFuncId(supFuncIds, func.getSupFuncId());
    }

    /**
     * 新增菜单
     *
     * @param funcName
     * @param funcCode
     * @param funcType
     * @param funcUrl
     * @param status
     * @return
     */
    public AjaxData save(Long supFuncId, String funcName, String funcCode, String funcIcon, String funcType, String funcUrl, String status, int sort, Long sysOperId) {
        SysFunc func = funcDao.getByCode(funcCode);
        if (null != func) {
            return AjaxData.failure("菜单编码已经存在！");
        }
        funcDao.save(supFuncId, funcName, funcCode, funcIcon, funcType, funcUrl, status, sort, sysOperId);
        return AjaxData.success();
    }

    /**
     * 修改菜单
     *
     * @param funcId
     * @param supFuncId
     * @param funcName
     * @param funcCode
     * @param funcType
     * @param funcUrl
     * @param status
     * @return
     */
    public AjaxData update(Long funcId, Long supFuncId, String funcName, String funcCode,
                           String funcIcon, String funcType, String funcUrl, String status, int sort, Long sysOperId) {
        funcDao.update(funcId, supFuncId, funcName, funcCode, funcIcon, funcType, funcUrl, status, sort, sysOperId);
        return AjaxData.success();
    }


    /**
     * 删除菜单
     *
     * @param roleId
     */
    public AjaxData delete(Long roleId) {
        SysFunc func = funcDao.getById(roleId);
        funcDao.delete(roleId);
        return AjaxData.success();
    }

    /**
     * 启用菜单
     *
     * @param funcId
     * @return
     */
    public AjaxData start(Long funcId, Long sysOperId) {
        funcDao.updateStatus(funcId, SysFunc.STATUS_START, sysOperId);
        return AjaxData.success();
    }

    /**
     * 停用菜单
     *
     * @param funcId
     * @return
     */
    public AjaxData stop(Long funcId, Long sysOperId) {
        funcDao.updateStatus(funcId, SysFunc.STATUS_STOP, sysOperId);
        return AjaxData.success();
    }

    /**
     * 查询菜单树
     *
     * @return
     */
    public AjaxData getTree() {
        List<SysFunc> funcs = funcDao.listAll();
        return AjaxData.success(FuncUtil.getTree(funcs));
    }


    /**
     * 批量启用
     *
     * @param funcIds
     * @return
     */
    public AjaxData batchStart(List<Long> funcIds, Long sysOperId) {
        Db.tx(() -> {
            for (Long funcId : funcIds) {
                funcDao.updateStatus(funcId, SysFunc.STATUS_START, sysOperId);
            }
            return true;
        });

        return AjaxData.success();
    }

    /**
     * 批量停用
     *
     * @param funcIds
     * @return
     */
    public AjaxData batchStop(List<Long> funcIds, Long sysOperId) {
        Db.tx(() -> {
            for (Long funcId : funcIds) {
                funcDao.updateStatus(funcId, SysFunc.STATUS_STOP, sysOperId);
            }
            return true;
        });

        return AjaxData.success();
    }
}
